/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function(head) {
    if (!head) return head;
    const set = new Set()
    let last = head;
    set.add(head.val)
    let curr = head.next;
    while(curr) {
        if (!set.has(curr.val)) {
            last.next = curr;
            last = last.next;
            set.add(curr.val);
        }
        curr = curr.next;
    }

    last.next = null;
    return head;
};


function ListNode(val, next = null) {
    this.val = val;
    this.next = next;
}

const head = new ListNode(1, new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(3)))))
const result = deleteDuplicates(head)
console.log(JSON.stringify(result))